package org.dromara.profiles.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.profiles.api.dto.DonorListDTO;
import org.dromara.profiles.api.dto.DonorQueryDTO;
import org.dromara.profiles.api.service.RemoteDonorService;
import org.dromara.profiles.domain.BusDonor;
import org.dromara.profiles.domain.vo.BusDonorVo;
import org.dromara.profiles.mapper.BusDonorMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 捐赠者 实施类
 *
 * @author congxing
 */
@Slf4j
@Service
@DubboService
@RequiredArgsConstructor
public class RemoteDonorServiceImpl implements RemoteDonorService {

    private final BusDonorMapper busDonorMapper;

    @Override
    public TableDataInfo<DonorListDTO> listDonor(DonorQueryDTO query, PageQuery pageQuery) {
        // 1. 先调本地一样的方法，拿到 Page<BusPartnerClinicVo>
        Page<BusDonorVo> voPage = busDonorMapper.queryPageList(
            pageQuery.build(),
            buildQueryWrapper(query)   // 把 DonorQueryDTO 转成 QueryWrapper
        );

        // 2. Vo -> DTO （字段少就手动抄，字段多就用 MapStruct/BeanUtils）
        List<DonorListDTO> dtoList = voPage.getRecords()
            .stream()
            .map(vo -> new DonorListDTO(
                vo.getId(),
                vo.getDonorName(),
                vo.getPhone()))
            .collect(Collectors.toList());

        // 3. 返回分页包装
        return TableDataInfo.build(dtoList, voPage.getTotal());
    }

    // 分页查询条件
    private LambdaQueryWrapper<BusDonor> buildQueryWrapper(DonorQueryDTO q) {
        return new LambdaQueryWrapper<BusDonor>()
            // 出生年份区间
            .ge(q.getBirthBegin() != null, BusDonor::getBirth, q.getBirthBegin())
            .le(q.getBirthEnd()   != null, BusDonor::getBirth, q.getBirthEnd())
            // 身高区间
            .ge(q.getHeightBegin() != null, BusDonor::getHeight, q.getHeightBegin())
            .le(q.getHeightEnd()   != null, BusDonor::getHeight, q.getHeightEnd())
            // 体重区间
            .ge(q.getWeightBegin() != null, BusDonor::getWeight, q.getWeightBegin())
            .le(q.getWeightEnd()   != null, BusDonor::getWeight, q.getWeightEnd())
            // 血型
            .eq(q.getBloodType() != null, BusDonor::getBloodType, q.getBloodType())
            // 瞳色
            .eq(q.getEyeColor() != null, BusDonor::getEyeColor, q.getEyeColor())
            // 发色
            .eq(q.getHairColor() != null, BusDonor::getHairColor, q.getHairColor())
            // 肤色
            .eq(q.getSkinTone() != null, BusDonor::getSkinTone, q.getSkinTone())
            // 体型
            .eq(q.getBodyShape() != null, BusDonor::getBodyShape, q.getBodyShape())
            // 族裔
            .eq(q.getEthnicity() != null, BusDonor::getEthnicity, q.getEthnicity())
            // 血统（模糊）
            .like(StringUtils.isNotBlank(q.getAncestry()), BusDonor::getAncestry, q.getAncestry())
            // 出生地（模糊）
            .like(StringUtils.isNotBlank(q.getBirthplace()), BusDonor::getBirthplace, q.getBirthplace())
            // 现居住地（模糊）
            .like(StringUtils.isNotBlank(q.getResidence()), BusDonor::getResidence, q.getResidence())
            // 职业（模糊）
            .like(StringUtils.isNotBlank(q.getOccupation()), BusDonor::getOccupation, q.getOccupation())
            // 运动习惯（模糊）
            .like(StringUtils.isNotBlank(q.getExerciseHabit()), BusDonor::getExerciseHabit, q.getExerciseHabit())
            // 性格特点（模糊）
            .like(StringUtils.isNotBlank(q.getPersonality()), BusDonor::getPersonality, q.getPersonality())
            // 婚姻状况
            .eq(q.getMaritalStatus() != null, BusDonor::getMaritalStatus, q.getMaritalStatus())
            // 特殊才能（模糊）
            .like(StringUtils.isNotBlank(q.getTalent()), BusDonor::getTalent, q.getTalent())
            // 自我介绍（模糊）
            .like(StringUtils.isNotBlank(q.getSelfIntro()), BusDonor::getSelfIntro, q.getSelfIntro())
            // 捐赠者类型
            .eq(q.getDonorType() != null, BusDonor::getDonorType, q.getDonorType());

    }
}
